package org.opensourceway.sbom.model.entity;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

/**
 * Describes a vulnerability.
 */
@Entity
@Table(indexes = {
        @Index(name = "vul_uk", columnList = "vul_id", unique = true)
})
public class Vulnerability {

    @Id
    @GeneratedValue(generator = "UUID")
    @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
    private UUID id;

    /**
     * Unique identifier of a vulnerability, e.g., a CVE number.
     */
    @Column(columnDefinition = "TEXT", name = "vul_id")
    private String vulId;

    /**
     * Description of a vulnerability.
     */
    @Column(columnDefinition = "TEXT")
    private String description;

    @Column(name = "record_time", columnDefinition = "TIMESTAMP WITH TIME ZONE")
    @CreationTimestamp
    private Timestamp recordTime;

    /**
     * Scores of a vulnerability.
     */
    @OneToMany(mappedBy = "vulnerability", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<VulScore> vulScores;

    /**
     * References of a vulnerability.
     */
    @OneToMany(mappedBy = "vulnerability", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<VulReference> vulReferences;

    /**
     * External vulnerability references of a package.
     */
    @OneToMany(mappedBy = "vulnerability", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<ExternalVulRef> externalVulRefs;

    public UUID getId() {
        return id;
    }

    public void setId(UUID id) {
        this.id = id;
    }

    public String getVulId() {
        return vulId;
    }

    public void setVulId(String vulId) {
        this.vulId = vulId;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Timestamp getRecordTime() {
        return recordTime;
    }

    public void setRecordTime(Timestamp recordTime) {
        this.recordTime = recordTime;
    }

    public List<VulScore> getVulScores() {
        return vulScores;
    }

    public void setVulScores(List<VulScore> vulScores) {
        if (Objects.isNull(this.vulScores)) {
            this.vulScores = vulScores;
        } else {
            this.vulScores.clear();
            this.vulScores.addAll(vulScores);
        }
    }

    public List<VulReference> getVulReferences() {
        return vulReferences;
    }

    public void setVulReferences(List<VulReference> vulReferences) {
        if (Objects.isNull(this.vulReferences)) {
            this.vulReferences = vulReferences;
        } else {
            this.vulReferences.clear();
            this.vulReferences.addAll(vulReferences);
        }
    }

    public List<ExternalVulRef> getExternalVulRefs() {
        return externalVulRefs;
    }

    public void setExternalVulRefs(List<ExternalVulRef> externalVulRefs) {
        if (Objects.isNull(this.externalVulRefs)) {
            this.externalVulRefs = externalVulRefs;
        } else {
            this.externalVulRefs.clear();
            this.externalVulRefs.addAll(externalVulRefs);
        }
    }
}
